﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLBH;
using Module.Category.BL;
using QLBH.DTO;

namespace Module.Category
{
    public partial class CustomerManagement : ViewBase
    {
        private static CustomerManagement instance;

        public static CustomerManagement Instance
        {
            get 
            {
                if (instance==null)
                {
                    instance = new CustomerManagement();
                }
                return CustomerManagement.instance; 
            }
            
        }
        public CustomerManagement()
            :base()
        {
            InitializeComponent();
            
        }

        private void tableLayoutPanel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void CustomerManagement_Load(object sender, EventArgs e)
        {
            try
            {
                dataGridViewX1.DataSource = CustomerManagementBL.Instance.GetAllCustomers();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            try
            {
                AddCustomer cust = new AddCustomer();
                if (cust.ShowDialog() == DialogResult.OK)
                {
                    CustomerManagementBL.Instance.AddCustomer(cust.NewCustomer);

                    dataGridViewX1.DataSource = CustomerManagementBL.Instance.GetAllCustomers();
        
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }           
        }

        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            try
            {
                dataGridViewX1.DataSource = CustomerManagementBL.Instance.GetAllCustomers();
        
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            
        }

        private void dataGridViewX1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            EditCustomer();
        }

        private void buttonEdit_Click(object sender, EventArgs e)
        {
            EditCustomer();
            
        }
        private void EditCustomer()
        {
            try
            {
                EditCustomer editcus = new EditCustomer();
                editcus.Customer = dataGridViewX1.CurrentRow.DataBoundItem as KhachHangDTO;

                if (editcus.ShowDialog() == DialogResult.OK)
                {
                    CustomerManagementBL.Instance.UpdateCustomer(editcus.Customer);

                    dataGridViewX1.DataSource = CustomerManagementBL.Instance.GetAllCustomers();

                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }
        private void dataGridViewX1_DataSourceChanged(object sender, EventArgs e)
        {
            if (dataGridViewX1.RowCount == 0)
            {
                buttonEdit.Enabled = false;
                buttonDelete.Enabled = false;
            }
            else
            {
                buttonEdit.Enabled = true;
                buttonDelete.Enabled = true;
            }
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            try
            {
                List<KhachHangDTO> selectedObjs = new List<KhachHangDTO>();
                foreach (DataGridViewRow row in dataGridViewX1.SelectedRows)
                {
                    selectedObjs.Add(row.DataBoundItem as KhachHangDTO);
                }

                if (selectedObjs.Count > 0)
                {
                    CustomerManagementBL.Instance.DeleteCustomers(selectedObjs);

                    dataGridViewX1.DataSource = CustomerManagementBL.Instance.GetAllCustomers();

                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            
        }
    }
}
